// 工厂方法模式 = 简单工厂模式 + 开闭原则
package main

import "fmt"

// 抽象类

type AbstractFruit interface {
	Show()
}

type AbstractFactory interface {
	CreateFruit() AbstractFruit // 生产水果类（抽象）的生产器方法
}

// 具体类

type Apple struct{}

func (a *Apple) Show() {
	fmt.Println("This is Apple")
}

type Banana struct{}

func (b *Banana) Show() {
	fmt.Println("This is Banana")
}

type AppleFactory struct{}

func (af *AppleFactory) CreateFruit() AbstractFruit {
	var fruit AbstractFruit
	fruit = new(Apple)
	return fruit
}

type BananaFactory struct{}

func (bf *BananaFactory) CreateFruit() AbstractFruit {
	var fruit AbstractFruit
	fruit = new(Banana)
	return fruit
}

func main() {
	// 易读不易写 <-----推荐----->
	var appleFactory AbstractFactory
	appleFactory = new(AppleFactory)
	var apple AbstractFruit
	apple = appleFactory.CreateFruit()
	apple.Show()

	// 易写不易读
	bananaFactory := new(BananaFactory)
	banana := bananaFactory.CreateFruit()
	banana.Show()
}
